package com.seventh.parkingrent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seventh.parkingrent.dao.UserMapper;
import com.seventh.parkingrent.domain.Record;
import com.seventh.parkingrent.dao.RecordMapper;
import com.seventh.parkingrent.domain.User;
import com.seventh.parkingrent.service.IRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-27
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService {

    @Resource
    private RecordMapper recordMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Record> findRecord(int userId) {
        QueryWrapper<Record> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Record> records = recordMapper.selectList(wrapper);
        return records;
    }

    @Override
    public Integer topUp(Double money, Integer id) {
        Record record = new Record();
        record.setUserId(id);
        record.setState("未支付");
        record.setPrice(new BigDecimal(money));
        recordMapper.insert(record);
        //返回充值订单id
        return record.getRecordId();
    }

    @Override
    public boolean okStatusById(Integer id) {
        Record record = recordMapper.selectById(id);
        record.setState("充值成功");
        int i = recordMapper.updateById(record);
        Integer userId = record.getUserId();
        User user = userMapper.selectById(userId);
        user.setBalance(user.getBalance().add(record.getPrice()));
        int q = userMapper.updateById(user);
        return i>0&&q>0;
    }
}
